home *** CD-ROM | disk | FTP | other *** search
- Path: ix.netcom.com!netnews
- From: jlilley@ix.netcom.com (John Lilley)
- Newsgroups: comp.lang.c++
- Subject: Re: Specifying Data Layout
- Date: 15 Mar 1996 02:45:02 GMT
- Organization: Netcom
- Message-ID: <4ialje$3s0@ixnews3.ix.netcom.com>
- References: <1996Mar14.173643.8651@nosc.mil>
- NNTP-Posting-Host: den-co10-19.ix.netcom.com
- Mime-Version: 1.0
- Content-Type: Text/Plain; charset=US-ASCII
- X-NETCOM-Date: Thu Mar 14 6:45:02 PM PST 1996
- X-Newsreader: WinVN 0.99.7
-
- In article <1996Mar14.173643.8651@nosc.mil>, sampson@cod.nosc.mil says...
- >
- > Is there some way of precisely specifying the memory layout of a
- >struct? In other words, can you say, "This struct is to occupy 3 words of
- >memory, element foo is to occupy bits 5-12 of word 1, element fern is to
- >occupy ..."? If it can be done with a struct, can it also be done with a
- >class (specifying locations for the data members only)? In this case, can
- >you also control the gizmo that is used to distinguish the various values
- >of a class hierarchy, both where it is to be allocated and the values to be
- >used to identify specific members of the hierarchy?
-
-
- First, remember that for simple, non-derived classes with no virtual
- methods, the data layout is exactly compatible with "C" for
- compatibility reasons. That being said, the only guarantee that
- the language makes is:
-
- 1) The elements will appear in memory in the order that they appear
- in the class/struct declaration.
-
- 2) The elements will be properly aligned according to the requirements
- of the machine architechure.
-
- PC compilers in 16-bit mode typically "pack" structures so that
- all elements are byte-aligned (no padding). On Intel Windows95
- and NT, the alignment is typically 2 bytes. There are compiler
- options to control the alignment. Alignment varies on other
- machines.
-
- In order to specify the bit locations of elements, I would first
- determine (experimentally if needed) the alignment for your target
- machine. Then use the mask-and-shift operations to extract the
- bits out of words. Bitfields don't make any guarantees about
- which bits get used, so explicit extraction is better.
-
- john lilley
-
-